package com.qianclass.talklive.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;
import android.widget.Toast;

import com.qianclass.talklive.R;
import com.qianclass.talklive.app.InterfaceBack.OnLikeListener;
import com.qianclass.talklive.app.bean.SortMemberBean;
import com.qianclass.talklive.app.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

public class SortMemberAdapter extends BaseAdapter implements SectionIndexer {
	private List<SortMemberBean> mList = null;
	private Context              mContext;
	private int                  mHostId;
	private int                  mUserId;
	private int                  mUserType;
	private int                  mRoomType;
	private int                  likenum1;
	private int                  lnum1;
	private List<SortMemberBean> mTeacherList;
	private List<SortMemberBean> mAssistantList;
	private List<SortMemberBean> mStudentList;
	private List<SortMemberBean> mInspectorList;
	private OnLikeListener       onLikeListener;
	public int mAnchorID;

	public SortMemberAdapter(Context mContext, List<SortMemberBean> list, int hostId, int userid, int userType, int roomType, OnLikeListener onLikeListener) {
		this.mContext = mContext;
		this.mList = list;
		this.mHostId = hostId;
		this.mUserId = userid;
		this.mUserType = userType;
		this.mRoomType = roomType;
		this.onLikeListener=onLikeListener;
		mAnchorID = mHostId;
	}

	/**
	 * 当ListView数据发生变化时,调用此方法来更新ListView
	 *
	 * @param list
	 */
	public void updateListView(List<SortMemberBean> list) {
		mList = list;
		notifyDataSetChanged();
	}

	public int getCount() {
		return mList != null ? mList.size() : 0;
	}

	public Object getItem(int position) {
		return mList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {

		mTeacherList = new ArrayList<>();
		mStudentList = new ArrayList<>();
		mAssistantList = new ArrayList<>();
		mInspectorList = new ArrayList<>();
		for (SortMemberBean sortMemberBean : mList) {
			if (sortMemberBean.getType() == 1) {
				mTeacherList.add(sortMemberBean);
			} else if(sortMemberBean.getType() == 2){
				mStudentList.add(sortMemberBean);
			} else if (sortMemberBean.getType() == 3) {
				mAssistantList.add(sortMemberBean);
			} else if(sortMemberBean.getType() == 4){
				mInspectorList.add(sortMemberBean);
			} else{
				mStudentList.add(sortMemberBean);
			}
		}

		ViewHolder viewHolder = null;
		ViewHolder viewHolder1 = null;
		final SortMemberBean memberBean = mList.get(position);

		for (SortMemberBean sortMemberBean : mList) {
			int likenum = sortMemberBean.getLikenum();


		}


		/*if (mRoomType == 3) {
			LogUtil.i("like","人头");
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sort_member, null);
				viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
				viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				//                viewHolder.tv_dznum = (TextView) convertView.findViewById(R.id.tv_dznum);
				viewHolder.iv_dz = (ImageView) convertView.findViewById(R.id.iv_dz);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			if (mTeacherList.size() != 0 && position < mTeacherList.size()) {

				viewHolder.tvLetter.setVisibility(View.GONE);
				viewHolder.tv_name.setText(mTeacherList.get(position).getUsername());
				viewHolder.tv_name.setTextColor(Color.parseColor("#FF4C56"));
				//                viewHolder.tv_dznum.setText(mTeacherList.get(position).getLikenum() + "");
				viewHolder.iv_dz.setImageResource(R.drawable.red_member_icon);
			} else if (mAssistantList.size() != 0 && position < mAssistantList.size() + mTeacherList.size()) {
				final int assistantPosition = position - mTeacherList.size();


				viewHolder.tvLetter.setVisibility(View.GONE);
				viewHolder.tv_name.setText(mAssistantList.get(assistantPosition).getUsername());
				//			viewHolder.tv_name.setTextColor(Color.parseColor("#FF0CFAAF")); 暂时先不用绿色
				viewHolder.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
				//                viewHolder.tv_dznum.setText(mAssistantList.get(assistantPosition).getLikenum() + "");
				viewHolder.iv_dz.setImageResource(R.drawable.red_member_icon);
			} else {
				//根据position获取分类的首字母的Char ascii值
				final int newPosition = position - mTeacherList.size() - mAssistantList.size();
				int section = getSectionForPosition(newPosition);

				//如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            *//*if(newPosition == getPositionForSection(section)){
				viewHolder.tvLetter.setVisibility(View.VISIBLE);
				viewHolder.tvLetter.setText(mStudentList.get(newPosition).getSortLetters());
			}else{
				viewHolder.tvLetter.setVisibility(View.GONE);
			}*//*
				viewHolder.tvLetter.setVisibility(View.GONE);

				viewHolder.tv_name.setText(mStudentList.get(newPosition).getUsername());
				viewHolder.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
				viewHolder.iv_dz.setImageResource(R.drawable.member_icon);
				//                viewHolder.tv_dznum.setText(mStudentList.get(newPosition).getLikenum() + "");
			}
		}else{//显示点赞功能
			LogUtil.i("like","---点赞---");
			if (convertView == null) {
				viewHolder1 = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sort_member_like, null);
				viewHolder1.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
				viewHolder1.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				viewHolder1.tv_dznum = (TextView) convertView.findViewById(R.id.tv_dznum);
				viewHolder1.iv_dz = (ImageView) convertView.findViewById(R.id.iv_dz);
				convertView.setTag(viewHolder1);
			} else {
				viewHolder1 = (ViewHolder) convertView.getTag();
			}

			if (mTeacherList.size() != 0 && position < mTeacherList.size()) {
				LogUtil.i("like", "mTeacherList "+mTeacherList.get(position).getLikenum());
				viewHolder1.tvLetter.setVisibility(View.GONE);
				viewHolder1.tv_name.setText(mTeacherList.get(position).getUsername());
				viewHolder1.tv_name.setTextColor(Color.parseColor("#FF4C56"));
				viewHolder1.tv_dznum.setText(mTeacherList.get(position).getLikenum() + "");
				viewHolder1.iv_dz.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtil.i("like","老师被点赞");
						onLikeListener.setLike();
					}
				});

			} else if (mAssistantList.size() != 0 && position < mAssistantList.size() + mTeacherList.size()) {
				final int assistantPosition = position - mTeacherList.size();

				LogUtil.i("like", "mAssistantList "+mAssistantList.get(assistantPosition).getLikenum());
				viewHolder1.tvLetter.setVisibility(View.GONE);
				viewHolder1.tv_name.setText(mAssistantList.get(assistantPosition).getUsername());
				//			viewHolder.tv_name.setTextColor(Color.parseColor("#FF0CFAAF")); 暂时先不用绿色
				viewHolder1.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
				//                LogUtil.i("like", "mAssistantList "+mAssistantList.get(position).getLikenum() + "");
				viewHolder1.tv_dznum.setText(mAssistantList.get(assistantPosition).getLikenum() + "");
				//                viewHolder.iv_dz.setImageResource(R.drawable.red_member_icon);
			} else {
				//根据position获取分类的首字母的Char ascii值
				final int newPosition = position - mTeacherList.size() - mAssistantList.size();
				int section = getSectionForPosition(newPosition);

				//如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            *//*if(newPosition == getPositionForSection(section)){
				viewHolder.tvLetter.setVisibility(View.VISIBLE);
				viewHolder.tvLetter.setText(mStudentList.get(newPosition).getSortLetters());
			}else{
				viewHolder.tvLetter.setVisibility(View.GONE);
			}*//*
				viewHolder1.tvLetter.setVisibility(View.GONE);
				LogUtil.i("like", "学生收到点赞数 "+mStudentList.get(newPosition).getLikenum());
				viewHolder1.tv_name.setText(mStudentList.get(newPosition).getUsername());
				viewHolder1.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
				//                viewHolder.iv_dz.setImageResource(R.drawable.member_icon);
				//                LogUtil.i("like", "mStudentList "+mStudentList.get(position).getLikenum() + "");
				viewHolder1.tv_dznum.setText(mStudentList.get(newPosition).getLikenum() + "");
			}


		}*/

		if (convertView == null) {
			viewHolder1 = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sort_member_like, null);
			viewHolder1.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
			viewHolder1.iv_menu = (ImageView) convertView.findViewById(R.id.iv_menu);
			viewHolder1.iv_member = (ImageView) convertView.findViewById(R.id.iv_member);
			viewHolder1.iv_speaker = (ImageView) convertView.findViewById(R.id.iv_speaker);
			viewHolder1.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder1.tv_praise_num = (TextView) convertView.findViewById(R.id.tv_praise_num);
			viewHolder1.rl_praise = (RelativeLayout) convertView.findViewById(R.id.rl_praise);

			convertView.setTag(viewHolder1);
		} else {
			viewHolder1 = (ViewHolder) convertView.getTag();
		}

		if (mRoomType == 2) {
			viewHolder1.rl_praise.setVisibility(View.VISIBLE);
		} else {
			viewHolder1.rl_praise.setVisibility(View.GONE);
		}

		viewHolder1.iv_menu.setVisibility((mUserId == mHostId) || (mUserType == 3) ? View.VISIBLE : View.INVISIBLE);

		if (mTeacherList.size() != 0 && position < mTeacherList.size()) {
			LogUtil.i("like", "mTeacherList "+mTeacherList.get(position).getLikenum());
			viewHolder1.tvLetter.setVisibility(View.GONE);
			viewHolder1.iv_member.setImageResource(R.drawable.icon_teacher);
			viewHolder1.tv_name.setText(mTeacherList.get(position).getUsername());
			viewHolder1.tv_name.setTextColor(Color.parseColor("#FF4C56"));
			viewHolder1.tv_praise_num.setText(mTeacherList.get(position).getLikenum() + "");
			viewHolder1.iv_menu.setVisibility((mRoomType == 3) && (mUserType == 1 || mUserType == 3) ? View.VISIBLE : View.INVISIBLE);

			if (mUserType == 2 || mUserType == 3) {
				viewHolder1.rl_praise.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtil.i("like", "老师被点赞");
						onLikeListener.setLike(mTeacherList.get(position).getUserid());
					}
				});
			} else if(mUserType == 1) {
				viewHolder1.rl_praise.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						Toast.makeText(mContext, mContext.getResources().getString(R.string.course_detail_praise_self), Toast.LENGTH_SHORT).show();
					}
				});
			}
			viewHolder1.iv_menu.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onLikeListener.onMenuClick(mTeacherList.get(position).getUserid(), mTeacherList.get(position).getType());
				}
			});
			if (mTeacherList.get(position).getUserid().equals(mAnchorID + "")) {
				viewHolder1.iv_speaker.setVisibility(View.VISIBLE);
			} else {
				viewHolder1.iv_speaker.setVisibility(View.GONE);
			}
		} else if (mAssistantList.size() != 0 && position < mAssistantList.size() + mTeacherList.size()) {
			final int assistantPosition = position - mTeacherList.size();

			LogUtil.i("like", "mAssistantList "+mAssistantList.get(assistantPosition).getLikenum());
			viewHolder1.tvLetter.setVisibility(View.GONE);
			viewHolder1.iv_member.setImageResource(R.drawable.icon_teacher);
			viewHolder1.tv_name.setText(mAssistantList.get(assistantPosition).getUsername());
			viewHolder1.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
			//viewHolder1.tv_praise_num.setText(mAssistantList.get(assistantPosition).getLikenum() + "");
			viewHolder1.rl_praise.setVisibility(View.GONE);

			//viewHolder1.iv_menu.setVisibility(mUserType == 1 && mRoomType == 3 ? View.VISIBLE : View.INVISIBLE);
			if (mRoomType == 2) {
				viewHolder1.iv_menu.setVisibility(mUserType == 1 ? View.VISIBLE : View.INVISIBLE);
			} else if (mRoomType == 3) {
				viewHolder1.iv_menu.setVisibility(mUserType == 1 || mUserType == 3 ? View.VISIBLE : View.INVISIBLE);
			}

			viewHolder1.iv_menu.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onLikeListener.onMenuClick(mAssistantList.get(assistantPosition).getUserid(), mAssistantList.get(assistantPosition).getType());
				}
			});
			if (mAssistantList.get(assistantPosition).getUserid().equals(mAnchorID + "")) {
				viewHolder1.iv_speaker.setVisibility(View.VISIBLE);
			} else {
				viewHolder1.iv_speaker.setVisibility(View.GONE);
			}
		}else if (mInspectorList.size() != 0 && position < mInspectorList.size() + mAssistantList.size() + mTeacherList.size()) {
			final int inspectorPosition = position - mAssistantList.size() - mTeacherList.size();

			viewHolder1.tvLetter.setVisibility(View.GONE);
			viewHolder1.iv_member.setImageResource(R.drawable.icon_inspector);
			viewHolder1.tv_name.setText(mInspectorList.get(inspectorPosition).getUsername());
			viewHolder1.tv_name.setTextColor(Color.parseColor("#6e6e6e"));
			viewHolder1.rl_praise.setVisibility(View.GONE);

			viewHolder1.iv_menu.setVisibility(View.GONE);
			viewHolder1.iv_speaker.setVisibility(View.GONE);
		} else {
			//根据position获取分类的首字母的Char ascii值

			final int newPosition = position - mTeacherList.size() - mAssistantList.size() - mInspectorList.size();
			//int section = getSectionForPosition(newPosition);

			//如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
            /*if(newPosition == getPositionForSection(section)){
				viewHolder.tvLetter.setVisibility(View.VISIBLE);
				viewHolder.tvLetter.setText(mStudentList.get(newPosition).getSortLetters());
			}else{
				viewHolder.tvLetter.setVisibility(View.GONE);
			}*/
			viewHolder1.tvLetter.setVisibility(View.GONE);
			LogUtil.i("like", "学生收到点赞数 "+mStudentList.get(newPosition).getLikenum());
			viewHolder1.iv_member.setImageResource(R.drawable.icon_student);
			viewHolder1.tv_name.setText(mStudentList.get(newPosition).getUsername());
			viewHolder1.tv_name.setTextColor(Color.parseColor("#4cc5fc"));
			viewHolder1.tv_praise_num.setText(mStudentList.get(newPosition).getLikenum() + "");

			if (mUserType == 1 || mUserType == 3) {
				viewHolder1.rl_praise.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						LogUtil.i("like","老师被点赞");
						onLikeListener.setLike(mStudentList.get(newPosition).getUserid());
					}
				});
			}else if(mUserType == 2){
				viewHolder1.rl_praise.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mStudentList.get(newPosition).getUserid().equals(mUserId + "")) {
							Toast.makeText(mContext, mContext.getResources().getString(R.string.course_detail_praise_self), Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(mContext, mContext.getResources().getString(R.string.course_detail_praise_same), Toast.LENGTH_SHORT).show();
						}
					}
				});
			}

			viewHolder1.iv_menu.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					onLikeListener.onMenuClick(mStudentList.get(newPosition).getUserid(), mStudentList.get(newPosition).getType());
				}
			});

			if (mStudentList.get(newPosition).getUserid().equals(mAnchorID + "")) {
				viewHolder1.iv_speaker.setVisibility(View.VISIBLE);
			} else {
				viewHolder1.iv_speaker.setVisibility(View.GONE);
			}
		}

		return convertView;
	}


	final static class ViewHolder {
		TextView  tvLetter;
		TextView  tv_name;
		TextView tv_praise_num;
		ImageView iv_member;
		ImageView iv_menu;
		ImageView iv_speaker;
		RelativeLayout rl_praise;

	}


	/**
	 * 根据ListView的当前位置获取分类的首字母的Char ascii值
	 */
	public int getSectionForPosition(int position) {
		return mStudentList.get(position).getSortLetters().charAt(0);
	}

	/**
	 * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
	 */
	public int getPositionForSection(int section) {
		for (int i = 0; i < mStudentList.size(); i++) {
			String sortStr = mStudentList.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}

		return -1;
	}

	/**
	 * 提取英文的首字母，非英文字母用#代替。
	 *
	 * @param str
	 * @return
	 */
	private String getAlpha(String str) {
		String sortStr = str.trim().substring(0, 1).toUpperCase();
		// 正则表达式，判断首字母是否是英文字母
		if (sortStr.matches("[A-Z]")) {
			return sortStr;
		} else {
			return "#";
		}
	}

	@Override
	public Object[] getSections() {
		return null;
	}
}